package com.baizhi.controller;


import com.auth0.jwt.interfaces.DecodedJWT;
import com.baizhi.entity.Admin;
import com.baizhi.entity.Menu;
import com.baizhi.service.IAdminRoleService;
import com.baizhi.service.IAdminService;
import com.baizhi.service.IMenuService;
import com.baizhi.util.JwtUtils;
import com.baizhi.util.ResultEntity;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author longyang
 * @since 2022-01-13
 */
@RestController
@RequestMapping("/resource")
public class MenuController {
   @Autowired
   private IMenuService iMenuService;
   @Autowired
   private IAdminRoleService iAdminRoleService;
    /**
     * 展示主页面中左侧菜单
     */
    @GetMapping("/showUserMenu")
    public ResultEntity<List<Menu>> showUserMenu(@RequestHeader("Authorization") String token){
        //根据当前登陆人的信息查询该用户的菜单信息
        //1.把菜单里面当前登陆人的所有菜单查询出来。
        DecodedJWT id = JwtUtils.verify(token);
        System.err.println(id);
        Integer userid = Integer.valueOf(id.getClaim("id").asString());
        System.err.println(userid);
        List<Menu> menus = iMenuService.selectMenu(userid);
        if (menus!=null){
            //2.我们要把这层级关系整理出来。
            List<Menu> menuList = menus.stream().filter((menu) -> menu.getType() == 0).peek(menu -> {
                //找到menu对象下面的所有的二级菜单
                Long menuId = menu.getMenuId();//一级菜单的id
                //过滤出来当前菜单下面的所有的二级菜单
                List<Menu> collect = menus.stream().filter((secodeMenu) -> secodeMenu.getParentId() == menuId).collect(Collectors.toList());
                menu.setMenuList(collect);
            }).collect(Collectors.toList());
            //登录成功
            for (Menu m: menuList) {
                System.err.println(m);
            }
            return ResultEntity.ok(menuList);
        }else {
            //登录失败
            return  ResultEntity.error();
        }

    }
    /**
     * 展示主页面中左侧菜单
     */
    @GetMapping("/selectAll")
    public ResultEntity<List<Menu>> selectAll(){
        //调用方法
        List<Menu> menus = iMenuService.selectMenuAll();
        if(menus!=null){
            //成功
            return ResultEntity.ok(menus);
        }else {
            //失败
            return  ResultEntity.error();
        }
    }
    /**
     * 根据权限标记查找权限信息
     */
    @GetMapping("/selectOne/{resourceId}")
    public ResultEntity<Menu> selectAll(@PathVariable("resourceId")Integer resourceId){
        //调用方法
        Menu menu = iAdminRoleService.selectMenuByUser(resourceId);
        if(menu!=null){
            //成功
            return ResultEntity.ok(menu);
        }else {
            //失败
            return  ResultEntity.error();
        }
    }
    /**
     * 获取权限树
     */
    @GetMapping("/showAllResourceTree")
    public ResultEntity<List<Menu>> showAllResourceTree(){
        List<Menu> menus = iMenuService.selectMenuAll();
        if (menus!=null){
            //2.把这层级关系整理出来。
            List<Menu> menuList = menus.stream().filter((menu) -> menu.getType() == 0).peek(menu -> {
                //找到menu对象下面的所有的二级菜单
                Long menuId = menu.getMenuId();//一级菜单的id
                //过滤出来当前菜单下面的所有的二级菜单
                List<Menu> collect = menus.stream().filter((secodeMenu) -> secodeMenu.getParentId() == menuId).peek(secodeMenu ->{
                    //找到menu对象下面的所有的三级菜单
                    Long menuId1 = secodeMenu.getMenuId();//二级级菜单的id
                    //过滤出来当前菜单下面的所有的三级菜单
                    List<Menu> collect1 = menus.stream().filter((threeMenu) -> threeMenu.getParentId() == menuId1).collect(Collectors.toList());
                    secodeMenu.setMenuList(collect1);//将找出的三级菜单存入对象
                }).collect(Collectors.toList());
                menu.setMenuList(collect);//将找出的二级菜单存入对象
            }).collect(Collectors.toList());
            //登录成功
            for (Menu m: menuList) {
                System.err.println(m);
            }
            return ResultEntity.ok(menuList);
        }else {
            //登录失败
            return  ResultEntity.error();
        }
    }
    //修改权限信息
    @PutMapping("update")
    public ResultEntity<Menu> updateMenu(Menu menu){
        Integer integer = iMenuService.updateMenu(menu);
        if (integer==1){
            //成功
            return ResultEntity.ok(menu);
        }else {
            //失败
            return  ResultEntity.error();
        }
    }
    //根据菜单标记查找该菜单下面的权限点
    @GetMapping("selectChildrenByPid/{resourceId}")
    public ResultEntity<List<Menu>> selectChildrenByPid(@PathVariable("resourceId")int resourceId){
        List<Menu> menus = iMenuService.selectChildrenByPid(resourceId);
        if (menus!=null){
            //成功
            return ResultEntity.ok(menus);
        }else {
            //失败
            return  ResultEntity.error();
        }
    }
}

